package blackjack.client;

import java.net.*;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.util.*;
import javax.swing.ImageIcon;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;
import blackjack.client.appletVisualAndLogic.CardDeck;
import blackjack.client.appletVisualAndLogic.ConnectionPanel;
import blackjack.client.appletVisualAndLogic.GameLogic;
import blackjack.client.appletVisualAndLogic.TopPanel;

@SuppressWarnings("serial")
public class ConnectionApplet extends JApplet{
	/*AKNA SUURUSED*/
	private int cWidth = 200;
	private int cHeight = 200;
	private int gWidth = 600;
	private int gHeight = 350;

	public int esimenesuri = 0;
	public int teinesuri = 0;
	public int param = 0;
	public int param2 = 0;
	public int points = 0;

	public String kokku = null;
	public String first = null;
	public String second = null;

	public CardDeck deck = new CardDeck();

	@SuppressWarnings("unchecked")
	public ArrayList esimeneKard = new ArrayList();
	@SuppressWarnings("unchecked")
	public ArrayList teineKard = new ArrayList();
	@SuppressWarnings("unchecked")
	public ArrayList pickedCards= new ArrayList();

	JLabel r = new JLabel();
	JLabel info = new JLabel();

	JTextField saatmine = new JTextField(15);

	public JButton newGame = new JButton(""); 
	public JButton quit = new JButton("");
	public JButton enough = new JButton("");
	public JButton getCard = new JButton(""); 
	JButton connect = new JButton("");

	Font f = new Font(null, Font.ITALIC, 15);

	private Socket socket;

	@SuppressWarnings("unchecked")
	public Collection message = new ArrayList(); // FIFO

	private BufferedReader in;
	private PrintWriter out = null;

	public Container cp = getContentPane();

	ConnectionPanel botPanel = new ConnectionPanel(this);
	public GameLogic mainPanel = new GameLogic(this);
	TopPanel topPanel = new TopPanel(this);

	/**
	 * Nuppude lisamine, algseisundi maaramine(false/true)
	 */
	public void init() {
		ImageIcon connectImg1 = new ImageIcon("images/connect1.jpg");

		connect.setIcon(connectImg1);
		connect.setIcon(connectImg1);	
		connect.setBackground(Color.BLACK);

		this.setSize(cWidth, cHeight);
		newGame.setEnabled(false);
		mainPanel.setLayout(new FlowLayout());
		botPanel.setLayout(new FlowLayout());
		getCard.setEnabled(false);
		enough.setEnabled(false);

		getCard.setVisible(false);
		enough.setVisible(false);
		newGame.setVisible(false);
		quit.setVisible(false);

		/*TOP panel BUTTONS*/
		topPanel.add(getCard);
		topPanel.add(enough);
		topPanel.add(newGame);
		topPanel.add(quit);

		cp.add(BorderLayout.NORTH, topPanel);
		cp.add(BorderLayout.CENTER, mainPanel);

		botPanel.add(r);
		botPanel.add(saatmine);
		botPanel.add(connect);
		cp.add(BorderLayout.CENTER, botPanel);
		connect.addActionListener(botPanel);
		getCard.addActionListener(mainPanel);

		quit.addActionListener( new ActionListener(){
			public void actionPerformed(ActionEvent e){
				System.exit(0);
			}
		});

		newGame.addActionListener( new ActionListener(){
			public void actionPerformed(ActionEvent e){
				kokku = null;			param = 0;
				first = null;			param2 = 0;
				second = null;			points = 0;
				esimenesuri = 0;		teinesuri = 0;
				esimeneKard.clear();	getCard.setEnabled(true);
				teineKard.clear();		enough.setEnabled(true);
				mainPanel.repaint();	pickedCards.clear();
			}
		});

		enough.addActionListener( new ActionListener(){
			public void actionPerformed(ActionEvent e){
				sendMessage("enough");
				getCard.setEnabled(false);
				enough.setEnabled(false);
			}
		});

	}

	/**
	 * To send a message
	 * @param message
	 */
	public void sendMessage(String message){
		out.println(message);
	}
	/**
	 * Kui sisseLogimine onnestus siis akna suurus ning nuppude visibility muutub
	 */
	public void connected() {
		this.setSize(gWidth, gHeight);
		botPanel.setVisible(false);
		connect.setEnabled(false);
		getCard.setVisible(true);
		enough.setVisible(true);
		newGame.setVisible(true);
		quit.setVisible(true);
	}

	/*GETTERS*/
	public JTextField getSaatmine(){ return saatmine; }
	public JButton getCard(){ return getCard; }
	public Socket getSocket(){ return socket; }
	public BufferedReader getin(){ return in; }
	public PrintWriter getout(){ return out; }
	public String getFirst() { return first; }
	public String getSecond() { return second; }
	@SuppressWarnings("unchecked")
	public ArrayList getPickedCards() { return pickedCards; }
	public int getParam() { return param; }
	public int getParam1() { return param2; }
	@SuppressWarnings("unchecked")
	public Collection getMessage() { return message; }
	@SuppressWarnings("unchecked")
	public ArrayList getEsimeneKard() { return esimeneKard; }
	@SuppressWarnings("unchecked")
	public ArrayList getTeineKard() { return teineKard; }
	public JButton getQuit() { return quit;	}
	public JButton getEnough() { return enough; }
	public int getPoints() { return points; }
	public CardDeck getDeck() { return deck; }
	public Object getOut() { return null; }
	public int getEsimeneSuri() { return esimenesuri; }
	public int getTeineSuri() {	return teinesuri; }
	
	/*SETTERS*/
	public void setSocket(Socket socket){ this.socket = socket;	}
	public void setin(BufferedReader in){ this.in = in; }
	public void setout(PrintWriter out){ this.out = out; }
	public void setKokku(String string) { this.kokku = string; }
	public void setFirst(String string) { this.first = string; }
	public void setSecond(String string) { this.second = string; }

}
